package com.sto.stoapp.activity.scanner;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.ErrorInfoAdapter;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.db.manager.ErrorRecordManager;
import com.sto.stoapp.http.response.upload.ErrorRecordInfo;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHomeActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 错误详情列表
 * 
 * @author cjb
 * 
 */
public class ErrorRecordInfoActivity extends StoHomeActivity {

	ListView listView;
	String action;
	EnumTableNames tableName;
	List<ErrorRecordInfo> infos = new ArrayList<ErrorRecordInfo>();;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.sto_inquirymenu_list);
		Bundle extras = getIntent().getExtras();
		action = extras.getString("busiType");
		tableName = (EnumTableNames) extras.get("EnumTableNames");

		this.setTitleBottom(
				getString(Integer.parseInt(tableName.getScanTypeName()))
						+ "错误列表", action);
		initView();
	}

	private void initView() {
		listView = (ListView) findViewById(R.id.dest_list);
		new QueryThread(this).start();
	}

	class QueryThread extends BaseThread {

		private Context context;

		public QueryThread(Context context) {
			super(context);
			this.context = context;
			showDialog();
		}

		@Override
		public void run() {

			if (tableName.getOpCode() == null) {
				showMsg("没有错误数据");
				cancelDialog();
				return;
			}
			final List<ErrorRecordInfo> list = ErrorRecordManager.getInstance(
					context).queryByOpCode(tableName.getOpCode(),
					StoFilePreference.getInstance(context).getSiteCode());
			if (list.size() == 0) {
				showMsg("没有错误数据");

			} else {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						infos.clear();
						infos.addAll(list);
						ErrorInfoAdapter adapter = new ErrorInfoAdapter(
								context, infos);
						listView.setAdapter(adapter);

					}
				});
			}
			cancelDialog();
			super.run();
		}

		private void showMsg(final String msg) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {

					Helper.showToast(context, msg);
				}
			});
		}
	}
}
